import axios from "axios";
// import Element from 'element-ui'
import router from "./router";
import store from "./store";
import { message } from "ant-design-vue";
// Vue.prototype.$message = message

// axios 请求的默认地址
axios.defaults.baseURL = "http://localhost:8081";
export default {
  posUrl: "http://localhost:8081"
};
// 前置拦截
axios.interceptors.request.use(config => {
  return config;
});

axios.interceptors.response.use(
  response => {
    let res = response.data;

    if (res.code === 200) {
      return response;
    } else if (res.code == 400) {
      message.error(response.data.msg);
      return Promise.reject(response.data.msg);
    } else if (res.code == 301) {
      return response;
    }
  },
  error => {
    console.log(error);
    if (error.response.data) {
      error.message = error.response.data.msg;
      // 防止查找不存在的用户
      if (error.response.data.msg === "用户不存在") {
        router.push("/");
      }
      if (error.response.data.msg === "账号或密码错误！") {
        // console.log('密码错误')
        // console.log(error.response)
        return error.response;
      }
      if (
        error.response.data.msg === "用户名已存在" ||
        error.response.data.msg === "邮箱已存在" ||
        error.response.data.msg === "注册失败"
      ) {
        return error.response;
      }
    }

    if (error.response.status === 401) {
      store.commit("REMOVE_INFO");
      router.push("/login");
    }

    message.error(error.message, 3);
    return Promise.reject(error);
  }
);
